package com.kaizzi.data.common.utils;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.apache.commons.lang3.StringUtils;
import org.apache.xmlbeans.impl.jam.JSourcePosition;
import org.omg.CORBA.PRIVATE_MEMBER;
import sun.security.util.PendingException;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.*;

/**
 * @author Kzen
 * @Description 时间 公共类
 * @date 2020/9/9
 */
public class DateUtil {

    private static final String DATE_FORMAT1 = "yyyyMMdd";
    private static final String DATE_FORMAT2 = "yyyy.MM.dd";
    private static final String DATE_FORMAT3 = "yyyy-MM-dd HH:mm:ss";
    private static final String DATE_FORMAT4 = "yyyy-MM-dd";
    private static final String DATE_FORMAT5 = "M.d";
    private static final String DATE_FORMAT6 = "yyyy.MM.dd HH:mm:ss";
    private static final String DATE_FORMAT7 = "yyyy.M.d HH:mm:ss";
    private static final String DATE_FORMAT10 = "yyyy-M-d HH:mm:ss";
    private static final String DATE_FORMAT8 = "MMdd";
    private static final String DATE_FORMAT9 = "yyyy/MM/dd";
    private static final String DATE_FORMAT11 = "yyyy/M/d HH:mm:ss";
    private static final String DATE_FORMAT12 = "MM/dd";
    private static final String DATE_FORMAT13 = "MM.dd";
    private static final String DATE_FORMAT14 = "yyyy/MM";
    private static final String START_DATE_SUFFIX = " 00:00:00";
    private static final String END_DATE_SUFFIX = " 23:59:59";
    private static final String POINT = ".";
    private static final String YEAR_FIRST_SUFFIX = ".01.01";
    private static final String UNIVERSAL_YEAR_PREFIX = "2020.";
    private static final String MID_LINE = "-";
    private static final String JOIN = "#";

    /**
     * @return 返回当前时间
     */
    public static LocalDateTime getNowDate(){
        return LocalDateTime.now();
    }

    //当前时间格式化
    public static String getDateTimeNow(){
        DateTimeFormatter df = getDateTimeFormatter(DATE_FORMAT3);
        return df.format(getNowDate());
    }

    /**
     * @return 返回上周末日期
     */
    public static LocalDateTime getLastSunday(){
        LocalDateTime nowDate = getNowDate();
        int var1 = nowDate.getDayOfWeek().getValue();
        return nowDate.minusDays(var1);
    }

    public static String getDateFormat24(String date){
        DateTimeFormatter df = getDateTimeFormatter(DATE_FORMAT3);
        DateTimeFormatter df1 = getDateTimeFormatter(DATE_FORMAT12);
        return df1.format(df.parse(date));
    }

    public static List<String> getDateList(){
        LocalDateTime tempDate = getLastSunday();
        DateTimeFormatter df = getDateTimeFormatter(DATE_FORMAT5);
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            list.add(df.format(tempDate));
            tempDate = tempDate.minusDays(1);
        }
        return list;
    }

    /**
     * 当前日期-7 --当前日期（yyyy.MM.dd-yyyy.MM.dd）
     * @return 2020.09.09-2020.09.09
     */
    public static String getPage1Date(){
        DateTimeFormatter df = getDateTimeFormatter(DATE_FORMAT2);
        LocalDateTime lastSunday = getLastSunday();
        LocalDateTime lastMonday = lastSunday.minusDays(6);
        return df.format(lastMonday) +"-"+df.format(lastSunday);
    }

    /**
     * 英文月份+数字年份
     * @return July 2020
     */
    public static String getUsDate(){
        LocalDateTime lastSunday = getLastSunday();
        DateTimeFormatter df1 = DateTimeFormatter.ofPattern("MMMM", Locale.ENGLISH);
        //当前时间
        return df1.format(lastSunday)+" "+lastSunday.getYear();
    }

    //英文月份
    public static String getUsDate1(){
        DateTimeFormatter df1 = DateTimeFormatter.ofPattern("MMMM", Locale.ENGLISH);
        return df1.format(getLastSunday());
    }

    //上月英文月份
    public static String getUsDate2(){
        DateTimeFormatter df1 = DateTimeFormatter.ofPattern("MMMM", Locale.ENGLISH);
        return df1.format(getLastSunday().minusMonths(1));
    }

    //本月数字
    public static String getNumDate(){
        DateTimeFormatter df1 = DateTimeFormatter.ofPattern("M");
        return df1.format(getLastSunday());
    }

    //上月数字
    public static String getNumDate1(){
        DateTimeFormatter df1 = DateTimeFormatter.ofPattern("M");
        return df1.format(getLastSunday().minusMonths(1));
    }

    /**
     * @return 格式化上周末日期 20200906
     */
    public static String getLastSundayFormat(){
        return getUniversalDateFormat(getLastSunday(),DATE_FORMAT1);
    }

    public static String getLastSundayFormat2(){
        return getUniversalDateFormat(getLastSunday(),DATE_FORMAT3);
    }

    /**
     * @return 上周末所在年初（一月一日）
     */
    public static String getYearFirstDay(){
        LocalDateTime lastSunday = getLastSunday();
        return lastSunday.getYear() + YEAR_FIRST_SUFFIX;
    }

    /**
     *
     * @param dateFormat 样式
     * @return 格式化
     */
    public static DateTimeFormatter getDateTimeFormatter(String dateFormat){
        return DateTimeFormatter.ofPattern(dateFormat);
    }

    /**
     * @param date 需要格式化日期
     * @return 格式化日期
     */
    public static String getDateFormat1(LocalDateTime date) {
        DateTimeFormatter dateTimeFormatter = getDateTimeFormatter(DATE_FORMAT2);
        return dateTimeFormatter.format(date);
    }

    public static List<String> getYtdDay(){
        String yearFirstDay = getYearFirstDay();
//        LocalDateTime lastSunday = getLastSunday();
        String dateFormat1 = getDateFormat1(getLastSunday());
        List<String> list = new ArrayList<>();
        list.add(yearFirstDay+START_DATE_SUFFIX);
        list.add(dateFormat1+END_DATE_SUFFIX);
        return list;
    }

    /**
     * @param var 需转化日期
     * @param var1 日期转化样式
     * @return 格式化日期
     */
    public static String getUniversalDateFormat(LocalDateTime var, String var1){
        DateTimeFormatter dateTimeFormatter = getDateTimeFormatter(var1);
        return dateTimeFormatter.format(var);
    }

    /**
     * @return 年初 - 上周末日期
     * 2020.01.01-2020.09.06
     */
    public static String getFirstDay2LastSunday(){
        String yearFirstDay = getYearFirstDay();
        LocalDateTime lastSunday = getLastSunday();
        String lastSundayStr = getDateFormat1(lastSunday);
        return yearFirstDay + "-" + lastSundayStr;
    }

    public static String getLastWeekDate(){
        LocalDateTime lastSunday = getLastSunday();
        LocalDateTime lastMonday = lastSunday.minusDays(6);
        DateTimeFormatter df = getDateTimeFormatter(DATE_FORMAT2);
        return df.format(lastMonday) + MID_LINE + df.format(lastSunday);
    }

    /**
     * @param date 1.1
     * @return 1990.01.01
     */
    public static String getTransTime2(String date){
        DateTimeFormatter df = getDateTimeFormatter(DATE_FORMAT7);
        DateTimeFormatter df2 = getDateTimeFormatter("MM.dd");

        LocalDateTime parse = LocalDateTime.parse(UNIVERSAL_YEAR_PREFIX + date + START_DATE_SUFFIX, df);
        if (LocalDateTime.now().getMonthValue() - parse.getMonthValue() < 0){
            //跨年情况 年份-1
            return (LocalDateTime.now().getYear() - 1) + POINT + df2.format(parse);
        }
        return LocalDateTime.now().getYear() + POINT + df2.format(parse);
    }

    /**
     * 获取需要日期集合
     * @param count 需要count周
     * @return 需要的周 集合 [1.1-1.2,1.3-1.5,1.6-1.8]
     */
    public static List<String> getDateList(int count) {
        LocalDateTime tempDate = getLastSunday();
        DateTimeFormatter df = getDateTimeFormatter(DATE_FORMAT5);
        List<String> dateList = new ArrayList<>();

        for (int i = 0; i< count ; i++){
            LocalDateTime startDate = tempDate.minusDays(6);
            dateList.add(df.format(startDate)+MID_LINE+df.format(tempDate));
            tempDate = tempDate.minusDays(7);
        }
        Collections.reverse(dateList);
        return dateList;
    }

    public static String getYtdDate(){
        DateTimeFormatter df = getDateTimeFormatter(DATE_FORMAT5);
        return "1.1-" + df.format(getLastSunday());
    }

    /**
     * @return 年初到现在 所有的周起始日期
     */
    public static List<String> getYearWeeks(){

        LocalDateTime tempDate = getLastSunday();
        List<String> dateList = new ArrayList<>();
        DateTimeFormatter df = getDateTimeFormatter(DATE_FORMAT4);

        while (tempDate.getYear() >= LocalDateTime.now().getYear()) {
            LocalDateTime startDate = tempDate.minusDays(6);
            dateList.add(df.format(startDate)+START_DATE_SUFFIX + JOIN + df.format(tempDate)+END_DATE_SUFFIX);
            tempDate = tempDate.minusDays(7);
        }
        Collections.reverse(dateList);
        return dateList;
    }

    public static List<String> getYearWeeks1(String var){
        LocalDateTime tempDate = getLastSunday();
        List<String> dateList = new ArrayList<>();
        DateTimeFormatter df = getDateTimeFormatter(DATE_FORMAT4);
        DateTimeFormatter df3 = getDateTimeFormatter(DATE_FORMAT3);
        LocalDateTime tempDate1 = LocalDateTime.parse(var,df3);

        while (tempDate.isAfter(tempDate1)) {
            LocalDateTime startDate = tempDate.minusDays(6);
            dateList.add(df.format(startDate)+START_DATE_SUFFIX + JOIN + df.format(tempDate)+END_DATE_SUFFIX);
            tempDate = tempDate.minusDays(7);
        }
        Collections.reverse(dateList);
        return dateList;
    }

    public static List<String> getMakeWeeks(String startDate){
        LocalDateTime tempDate = getLastSunday();
        DateTimeFormatter df = getDateTimeFormatter(DATE_FORMAT4);
        DateTimeFormatter df3 = getDateTimeFormatter(DATE_FORMAT3);
        LocalDateTime parse = LocalDateTime.parse(startDate + START_DATE_SUFFIX,df3);

        int dayOfYear = parse.getDayOfYear();
        List<String> dateList = new ArrayList<>();

        while (tempDate.getDayOfYear() > dayOfYear){
            LocalDateTime aDate = tempDate.minusDays(6);
            dateList.add(df.format(aDate)+START_DATE_SUFFIX + JOIN + df.format(tempDate)+END_DATE_SUFFIX);
            tempDate = tempDate.minusDays(7);
        }
        Collections.reverse(dateList);
        return dateList;
    }

    /**
     * 一周每天日期
     * @param startDate 开始时间
     * @return 一周的时间
     */
    public static List<String> getWeekDays(String startDate){
        List<String> resultList = new ArrayList<>();
        DateTimeFormatter df = getDateTimeFormatter(DATE_FORMAT3);
        DateTimeFormatter df2 = getDateTimeFormatter(DATE_FORMAT9);
        LocalDateTime start = LocalDateTime.parse(startDate, df);

        for (int i=0;i<7;i++){
            LocalDateTime localDateTime = start.plusDays(i);
            resultList.add(df2.format(localDateTime));
        }
        return resultList;
    }

    /**
     *
     * @param oldDate 8.3-8.9
     * @return ["2019.12.30 00:00:00","2020.01.09 23:59:59"]
     */
    public static List<String> getTransTime(String oldDate) {

        List<String> dateList = new ArrayList<>();

        String join = StringUtils.join(LocalDateTime.now().getYear());
        String[] split = oldDate.split("-");
        String time1 =join + POINT + split[0] + START_DATE_SUFFIX;
        String time2 =join + POINT + split[1] + END_DATE_SUFFIX;

        DateTimeFormatter df = getDateTimeFormatter(DATE_FORMAT7);
        LocalDateTime date2 = LocalDateTime.parse(time2, df);
        LocalDateTime date = LocalDateTime.parse(time1, df);
        //跨年情况特殊处理
        if (date2.getMonthValue() < date.getMonthValue()) {
            String join1 = StringUtils.join(Integer.parseInt(join) - 1);
            time1 = join1 + POINT + split[0] + START_DATE_SUFFIX;
            time2 = join + POINT + split[1] + END_DATE_SUFFIX;
            date = LocalDateTime.parse(time1, df);
            date2 = LocalDateTime.parse(time2, df);
        }

        if (date2.getMonthValue() > LocalDateTime.now().getMonthValue()){
            String join1 = StringUtils.join(Integer.parseInt(join) - 1);
            time2 = join1 + POINT + split[1] + END_DATE_SUFFIX;
            date2 = LocalDateTime.parse(time2, df);
        }
        if (date.getMonthValue() > LocalDateTime.now().getMonthValue()){
            String join1 = StringUtils.join(Integer.parseInt(join) - 1);
            time1 = join1 + POINT + split[0] + START_DATE_SUFFIX;
            date = LocalDateTime.parse(time1, df);
        }

        DateTimeFormatter f2 = getDateTimeFormatter(DATE_FORMAT3);
        dateList.add(f2.format(date));
        dateList.add(f2.format(date2));
        return dateList;
    }

    /**
     * @return 年初第一天
     */
    public static String getYearFirstDate(){
        LocalDateTime lastSunday = getLastSunday();
        return lastSunday.getYear() + YEAR_FIRST_SUFFIX +START_DATE_SUFFIX;
    }

    public static String getDateFormat37(String date){
        DateTimeFormatter df = getDateTimeFormatter(DATE_FORMAT3);
        DateTimeFormatter df1 = getDateTimeFormatter(DATE_FORMAT5);
        return df1.format(df.parse(date));
    }

    /**
     *
     * @param oldDate 8.3-8.9
     * @return ["2019.12.30 00:00:00","2020.01.09 23:59:59"]
     */
    public static String getTransTime3(String oldDate) {

        List<String> dateList = new ArrayList<>();

        String join = StringUtils.join(LocalDateTime.now().getYear());
        String[] split = oldDate.split("-");
        String time1 =join + POINT + split[0] + START_DATE_SUFFIX;
        String time2 =join + POINT + split[1] + END_DATE_SUFFIX;

        DateTimeFormatter df = getDateTimeFormatter(DATE_FORMAT7);
        LocalDateTime date2 = LocalDateTime.parse(time2, df);
        LocalDateTime date = LocalDateTime.parse(time1, df);
        //跨年情况特殊处理
        if (date2.getMonthValue() < date.getMonthValue()) {
            String join1 = StringUtils.join(Integer.parseInt(join) - 1);
            time1 = join1 + POINT + split[0] + START_DATE_SUFFIX;
            time2 = join + POINT + split[1] + END_DATE_SUFFIX;
            date = LocalDateTime.parse(time1, df);
            date2 = LocalDateTime.parse(time2, df);
        }

        DateTimeFormatter f2 = getDateTimeFormatter(DATE_FORMAT2);
        return f2.format(date) + MID_LINE + f2.format(date2);
    }
    /**
     *
     * @param oldDate 8.3-8.9
     * @return ["2019.12.30 00:00:00","2020.01.09 23:59:59"]
     */
    public static String getTransTime4(String oldDate) {

        List<String> dateList = new ArrayList<>();

        String join = StringUtils.join(LocalDateTime.now().getYear());
        String[] split = oldDate.split("-");
        String time1 =join + POINT + split[0] + START_DATE_SUFFIX;
        String time2 =join + POINT + split[1] + END_DATE_SUFFIX;

        DateTimeFormatter df = getDateTimeFormatter(DATE_FORMAT7);
        LocalDateTime date2 = LocalDateTime.parse(time2, df);
        LocalDateTime date = LocalDateTime.parse(time1, df);
        //跨年情况特殊处理
        if (date2.getMonthValue() < date.getMonthValue()) {
            String join1 = StringUtils.join(Integer.parseInt(join) - 1);
            time1 = join1 + POINT + split[0] + START_DATE_SUFFIX;
            time2 = join + POINT + split[1] + END_DATE_SUFFIX;
            date = LocalDateTime.parse(time1, df);
            date2 = LocalDateTime.parse(time2, df);
        }
        if (date2.getMonthValue() > LocalDateTime.now().getMonthValue()){
            String join1 = StringUtils.join(Integer.parseInt(join) - 1);
            time2 = join1 + POINT + split[1] + END_DATE_SUFFIX;
            date2 = LocalDateTime.parse(time2, df);
        }
        if (date.getMonthValue() > LocalDateTime.now().getMonthValue()){
            String join1 = StringUtils.join(Integer.parseInt(join) - 1);
            time1 = join1 + POINT + split[0] + START_DATE_SUFFIX;
            date = LocalDateTime.parse(time1, df);
        }

        DateTimeFormatter f2 = getDateTimeFormatter(DATE_FORMAT5);
        return f2.format(date) + MID_LINE + f2.format(date2);
    }

    /**
     *
     * @param oldDate 8.3-8.9
     * @return ["2019.12.30 00:00:00","2020.01.09 23:59:59"]
     */
    public static String getTransTime5(String oldDate) {

        List<String> dateList = new ArrayList<>();

        String join = StringUtils.join(LocalDateTime.now().getYear());
        String[] split = oldDate.split("-");
        String time1 =join + POINT + split[0] + START_DATE_SUFFIX;
        String time2 =join + POINT + split[1] + END_DATE_SUFFIX;

        DateTimeFormatter df = getDateTimeFormatter(DATE_FORMAT7);
        LocalDateTime date2 = LocalDateTime.parse(time2, df);
        LocalDateTime date = LocalDateTime.parse(time1, df);
        //跨年情况特殊处理
        if (date2.getMonthValue() < date.getMonthValue()) {
            String join1 = StringUtils.join(Integer.parseInt(join) - 1);
            time1 = join1 + POINT + split[0] + START_DATE_SUFFIX;
            time2 = join + POINT + split[1] + END_DATE_SUFFIX;
            date = LocalDateTime.parse(time1, df);
            date2 = LocalDateTime.parse(time2, df);
        }

        DateTimeFormatter f2 = getDateTimeFormatter(DATE_FORMAT13);
        return f2.format(date) + MID_LINE + f2.format(date2);
    }

    public static String getDateFormat9(String startDate,String endDate){
        DateTimeFormatter df = getDateTimeFormatter(DATE_FORMAT8);
        DateTimeFormatter df2 = getDateTimeFormatter(DATE_FORMAT3);
        LocalDateTime start = LocalDateTime.parse(startDate, df2);
        LocalDateTime end = LocalDateTime.parse(endDate, df2);
        return df.format(start)+ MID_LINE + df.format(end);
    }

    public static String getDateFormat10(String startDate,String endDate){
        DateTimeFormatter df = getDateTimeFormatter(DATE_FORMAT5);
        DateTimeFormatter df2 = getDateTimeFormatter(DATE_FORMAT3);
        LocalDateTime start = LocalDateTime.parse(startDate, df2);
        LocalDateTime end = LocalDateTime.parse(endDate, df2);
        return df.format(start)+ MID_LINE + df.format(end);
    }

    // page 17
    public static List<String> getMonthDate(long add){

        LocalDateTime lastSunday = getLastSunday();
        LocalDateTime lastMonth = lastSunday.minusMonths(add);
        List<String> monthList = new ArrayList<>();
        DateTimeFormatter df = getDateTimeFormatter(DATE_FORMAT3);
        DateTimeFormatter df2 = getDateTimeFormatter(DATE_FORMAT10);
        DateTimeFormatter df3 = getDateTimeFormatter(DATE_FORMAT4);

        String startDay = lastMonth.getYear()+MID_LINE+lastMonth.getMonthValue()+MID_LINE+"01"+START_DATE_SUFFIX;
//        String endDay = lastMonth.getYear()+MID_LINE+lastMonth.getMonthValue()+MID_LINE+"31"+END_DATE_SUFFIX;
        LocalDateTime beforeStartDay = LocalDateTime.parse(startDay, df2).plusMonths(1).minusSeconds(1);
        String endDay = df.format(beforeStartDay);
//        String endDay =
        if (add == 0){
            endDay = df3.format(lastMonth)+END_DATE_SUFFIX;
        }

        monthList.add(startDay);
        monthList.add(endDay);

        return monthList;
    }

    /**
     * @param s jeishu
     * @param s1 kaishi
     * @param s2 bijiaoshijian
     * @return 两个时间比较 时间格式：1990-01-01 00:00:00
     */
    public static boolean Compare(String s,String s2, String s1) {
        DateTimeFormatter df = getDateTimeFormatter(DATE_FORMAT3);
        LocalDateTime before = LocalDateTime.parse(s, df);
        LocalDateTime after = LocalDateTime.parse(s1, df);
        LocalDateTime other = LocalDateTime.parse(s2, df);
        return before.getDayOfYear() < after.getDayOfYear() || other.getDayOfYear() < after.getDayOfYear();
    }

    /**
     *
     * @param startDate 8/9
     * @param endDate 8/9
     * @return 2020-08-09
     */
    public static List<String> getDateFormat20(String startDate,String endDate){

        int year = LocalDateTime.now().getYear();
        DateTimeFormatter df = getDateTimeFormatter(DATE_FORMAT11);
        DateTimeFormatter df2 = getDateTimeFormatter(DATE_FORMAT3);

        String tempStrart = year+"/"+startDate+START_DATE_SUFFIX;
        String format = df2.format(LocalDateTime.parse(tempStrart, df));

        String tempEnd = year +"/" +endDate +END_DATE_SUFFIX;
        String format1 = df2.format(LocalDateTime.parse(tempEnd, df));

        List<String> list = new ArrayList<>();
        list.add(format);
        list.add(format1);
        return list;
    }

    /**
     *
     * @param date 8/9
     * @return 08/09
     */
    public static String getDateFormat12(String date) {
        int year = LocalDateTime.now().getYear();
        DateTimeFormatter df = getDateTimeFormatter(DATE_FORMAT11);
        DateTimeFormatter df2 = getDateTimeFormatter(DATE_FORMAT12);

        String tempStart = year+"/"+date+START_DATE_SUFFIX;
        return df2.format(LocalDateTime.parse(tempStart, df));
    }

    //此刻long
    public static String getDateLong(){
        return StringUtil.join(System.currentTimeMillis());
    }

    /**
     * page10 单独使用
     */
    public static List<String> getDateMonth(){

        DateTimeFormatter df = getDateTimeFormatter(DATE_FORMAT14);
        List<String> dateList = new ArrayList<>();
        //start Date 2019/12至今
        String startDate = "2019-12-01T00:00:00";
        LocalDateTime tempDate = LocalDateTime.parse(startDate);
        while (LocalDateTime.now().isAfter(tempDate)){
            dateList.add(df.format(tempDate));
            tempDate = tempDate.plusMonths(1);
        }
        return dateList;
    }

    public static List<String> getDateValue(){
        LocalDateTime tempDate = getLastSunday();
        DateTimeFormatter df = getDateTimeFormatter(DATE_FORMAT12);
        List<String> dateList = new ArrayList<>();
        for (int i = 0; i < 7 ; i++) {
            dateList.add(df.format(tempDate));
            tempDate = tempDate.minusDays(1);
        }
        return dateList;
    }

    public static void main(String[] args) {

        long l = System.currentTimeMillis();

        List<String> dateMonth = getYtdDay1();
        System.out.println(JsonUtil.serialize(dateMonth));

        long l1 = System.currentTimeMillis();
        System.out.println("耗时： "+(l1-l)+"ms");


    }

    public static List<String> getYtdDay1(){
//        String yearFirstDay = getYearFirstDay();
//        LocalDateTime lastSunday = getLastSunday();
        String dateFormat1 = getDateFormat1(getLastSunday());
        List<String> list = new ArrayList<>();
        list.add("2020.01.01 00:00:00");
        list.add(dateFormat1+END_DATE_SUFFIX);
        return list;
    }

    //10.5
    public static String getRealDay(String date) {
//        date.split("")
        return null;
    }
}
